package cn.edu.hbpu.service.impl;

import cn.edu.hbpu.common.R;
import cn.edu.hbpu.entity.SecondCategory;
import cn.edu.hbpu.entity.ThirdCategory;
import cn.edu.hbpu.handler.CustomException;
import cn.edu.hbpu.mapper.SecondCategoryMapper;
import cn.edu.hbpu.mapper.ThirdCategoryMapper;
import cn.edu.hbpu.service.ThirdCategoryService;
import cn.edu.hbpu.utils.DateTimeFormat;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author lcy
 * @date 2022/8/16 19:50
 * @description
 */
@Service
public class ThirdCategoryServiceImpl implements ThirdCategoryService {

    @Autowired
    private ThirdCategoryMapper thirdCategoryMapper;

    @Autowired
    private SecondCategoryMapper secondCategoryMapper;

    @Override
    public R<String> deleteThirdCategory(String tId) {
        if (Objects.isNull(tId)) {
            throw new CustomException("请传入二级分类id");
        }
        // 1. 删除用户
        thirdCategoryMapper.deleteById(tId);
        // 2. 返回结果
        return new R<>(HttpStatus.HTTP_OK, "删除二级分类成功");
    }

    @Override
    public R<Page<ThirdCategory>> pagingQuery(String searchField, String searchText, String startDateTime, String endDateTime, int page, int pageSize) {
        // 1. 非空验证
        if (Objects.isNull(searchField) || Objects.isNull(searchText)) {
            throw new CustomException("请传入搜索字段或文本");
        }
        LocalDateTime start = null;
        LocalDateTime end = null;
        // 2. 将日期进行转换
        if (!Objects.isNull(startDateTime) && !Objects.isNull(endDateTime)) {
            start = DateTimeFormat.parseTimeToUtc(startDateTime);
            end = DateTimeFormat.parseTimeToUtc(endDateTime);
        }
        // 3. 将searchField转为下划线
        searchField = StringUtils.camelToUnderline(searchField);

        // 4. 创建Page对象
        Page<ThirdCategory> pageInfo = new Page<>(page, pageSize);

        List<ThirdCategory> thirdCategoryList = new ArrayList<>();
        // 5. 判断searchField的字段类型
        if (searchField.isEmpty()) {
            // 5.1 说明都不是，正常分页查询
            thirdCategoryList = thirdCategoryMapper.pagingThirdCategoryWithSecondCategoryName((page - 1) * pageSize, pageSize);
            // 5.1.1 查询总数据条数
            Integer totalCount = thirdCategoryMapper.selectCount(null);
            // 5.1.2 将总数据条数保存到pageInfo对象中
            pageInfo.setTotal(totalCount);
        } else if ("create_time".equals(searchField) || "update_time".equals(searchField)) {
            // 5.2 创建时间和更新时间字段
            thirdCategoryList = thirdCategoryMapper.pagingThirdCategoryWithSecondCategoryNameByDateTime(searchField, start, end, (page - 1) * pageSize, pageSize);
            // 5.2.1 查询满足创建时间或更新时间条件的数据条数
            Integer totalCount = thirdCategoryMapper.selectTotalCountByDateTime(searchField, start, end);
            // 5.2.2 将总数据条数保存到pageInfo对象中
            pageInfo.setTotal(totalCount);
        } else if ("s_name".equals(searchField)) {
            // 5.3 根据sName二级分类名搜索其下的所有三级分类
            // 5.3.1 创建二级分类的询构造器
            LambdaQueryWrapper<SecondCategory> secondCategoryWrapper = new LambdaQueryWrapper<>();
            // 5.3.2 查询二级分类名等于searchText数据行
            secondCategoryWrapper.eq(SecondCategory::getSName, searchText);
            // 5.3.3 查询
            SecondCategory secondCategory = secondCategoryMapper.selectOne(secondCategoryWrapper);
            if (!Objects.isNull(secondCategory)) {
                // 5.3.4 获取二级分类id
                String sId = secondCategory.getSId();
                // 5.3.5 创建三级分类的查询构造器
                LambdaQueryWrapper<ThirdCategory> thirdCategoryWrapper = new LambdaQueryWrapper<>();
                // 5.3.6 查询三级分类中sId等于searchText二级分类对应的sId
                thirdCategoryWrapper.eq(ThirdCategory::getSId, sId);
                // 5.3.7 查询该二级分类下所有的三级分类
                thirdCategoryMapper.selectPage(pageInfo, thirdCategoryWrapper);
                thirdCategoryList = pageInfo.getRecords();
                // 5.3.8 将二级分类名set到secondCategoryList中
                for (ThirdCategory thirdCategory : thirdCategoryList) {
                    thirdCategory.setSName(searchText);
                }
                // 5.3.9 返回结果
                return new R<>(HttpStatus.HTTP_OK, pageInfo);
            }
        } else {
            // 5.3 其它字段
            thirdCategoryList = thirdCategoryMapper.pagingThirdCategoryWithSecondCategoryNameByOtherField(searchField, searchText, (page - 1) * pageSize, pageSize);
            // 5.3.1 查询满足其它字段条件的数据条数
            Integer totalCount = thirdCategoryMapper.selectTotalCountByOtherField(searchField, searchText);
            // 5.3.2 将总数据条数保存到pageInfo对象中
            pageInfo.setTotal(totalCount);
        }
        // 6. 将数据添加到pageInfo中
        pageInfo.setRecords(thirdCategoryList);
        // 7. 返回结果
        return new R<>(HttpStatus.HTTP_OK, pageInfo);
    }
}
